//
//  LTQVistorView.swift
//  WeiBoProject
//
//  Created by MAC on 16/2/25.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
import SnapKit
// 由于注册和登录需要跳转控制器，UIView不能实现这个功能，通过代理来完成
protocol LTQVistorDelegate: NSObjectProtocol{
    
//    定义代理方法
    func didLogin()
    
    func didRegister()

}

class LTQVistorView: UIView {
    
//    设置代理属性
    weak var delegate: LTQVistorDelegate?
    
//    初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        给view设置背景色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
//        添加子控件和约束
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    添加子控件和约束
    private func setupUI(){
        
        addSubview(homeIconView)//旋转图片
        addSubview(maskImageView)//遮罩
        addSubview(iconImageView)//小房子
        addSubview(noticeLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
            
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
            
        }
        
        noticeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconView.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(noticeLabel.snp_left)
            make.top.equalTo(noticeLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(noticeLabel.snp_right)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        maskImageView.snp_makeConstraints(closure: { (make) -> Void in
            make.center.equalTo(self.snp_center)
        })
            
        }
        
    }
    
//    修改访客主页的信息
    func setupInfo(message:String,image:String,animation:Bool){
        
        noticeLabel.text = message
        iconImageView.image = UIImage(named: image)
        
//        如果有动画执行，home图片不隐藏
        if animation{
            homeIconView.hidden = false
            startAnimation()
//           没有动画 隐藏
        }else{
            
            homeIconView.hidden = true
            
        }
        
    }

//MARK -- 首页访客动画
    func startAnimation (){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_2_PI
        
        animation.repeatCount = MAXFLOAT
//        切换视图时，动画仍然在执行
        animation.removedOnCompletion = false
        
        animation.duration = 5
        
        homeIconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //    懒加载方式设置转动图片
    private lazy var homeIconView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        img.sizeToFit()
        
        return img
        
        }()
    
    //    首页小房子
   private lazy var iconImageView: UIImageView = {
       
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        img.sizeToFit()
        
        return img
    
    }()

//    展示文字
   private lazy var noticeLabel: UILabel = {
       
        let lb = UILabel()
        
        lb.font = UIFont.systemFontOfSize(14)
        lb.textColor = UIColor.darkGrayColor()
        lb.textAlignment = NSTextAlignment.Center
 
        lb.numberOfLines = 0
        
        lb.sizeToFit()
        
        return lb
        
    }()
    
   private lazy var registerButton:UIButton = {
       
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    private lazy var loginButton:UIButton = {
       
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn

    }()
    
    
//    遮罩视图
    
    private lazy var maskImageView:UIImageView = {
       
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        img.sizeToFit()
        
        return img
    }()
    
    func registerClick (){
//        调用代理的方法
        delegate?.didRegister()
    }
    
    func loginClick (){
//        调用代理地方
        delegate?.didLogin()
    }
    
    
}


